﻿using System.Configuration;

namespace Altairis.IdentityToolkit.Configuration {

    /// <summary>
    /// Class representing configuration of Windows Live ID subsystem.
    /// </summary>
    public class LiveConfiguration : ConfigurationSection {

        /// <summary>
        /// Gets or sets the configuration of the Windows Live ID sign in service.
        /// </summary>
        /// <value>Configuration of the Windows Live ID sign in service.</value>
        [ConfigurationProperty("service", IsRequired = true)]
        public ServiceElement Service {
            get { return (ServiceElement)this["service"]; }
            set { this["service"] = value; }
        }

        /// <summary>
        /// Gets or sets the Configuration of the Windows Live ID callback handler.
        /// </summary>
        /// <value>Configuration of the Windows Live ID callback handler.</value>
        [ConfigurationProperty("handler", IsRequired = false)]
        public HandlerElement Handler {
            get { return (HandlerElement)this["handler"]; }
            set { this["handler"] = value; }
        }

    }

    /// <summary>
    /// Class representing configuration of the Windows Live ID callback handler.
    /// </summary>
    public class HandlerElement : ConfigurationElement {

        /// <summary>
        /// Gets or sets the maximum allowed age of ticket in minutes. Default value is 5 minutes.
        /// </summary>
        /// <value>The maximum allowed age of ticket in minutes.</value>
        [ConfigurationProperty("maxTicketAge", IsRequired = false, DefaultValue = 5)]
        [IntegerValidator(MinValue = 0)]
        public int MaxTicketAge {
            get { return (int)this["maxTicketAge"]; }
            set { this["maxTicketAge"] = value; }
        }

        /// <summary>
        /// Gets or sets the time in minutes after which the ClaimSet is removed from cache.
        /// </summary>
        /// <value>The cache timeout in minutes.</value>
        [ConfigurationProperty("cacheTimeout", IsRequired = false, DefaultValue = 5)]
        [IntegerValidator(MinValue = 1)]
        public int CacheTimeout {
            get { return (int)this["cacheTimeout"]; }
            set { this["cacheTimeout"] = value; }
        }

        /// <summary>
        /// Gets or sets the logout page URL.
        /// </summary>
        /// <value>The logout page URL.</value>
        [ConfigurationProperty("logoutUrl", IsRequired = false, DefaultValue = "~/Default.aspx")]
        public string LogoutUrl {
            get { return (string)this["logoutUrl"]; }
            set { this["logoutUrl"] = value; }
        }

        /// <summary>
        /// Gets or sets the failure page URL.
        /// </summary>
        /// <value>The failure page URL.</value>
        [ConfigurationProperty("failureUrl", IsRequired = false, DefaultValue = "~/Login.aspx?Reason=failure")]
        public string FailureUrl {
            get { return (string)this["failureUrl"]; }
            set { this["failureUrl"] = value; }
        }

        /// <summary>
        /// Gets or sets the URL of the page used when Live ID ticket timed out.
        /// </summary>
        /// <value>The URL of the page used when Live ID ticket timed out.</value>
        [ConfigurationProperty("timeoutUrl", IsRequired = false, DefaultValue = "~/Login.aspx?Reason=timeout")]
        public string TimeoutUrl {
            get { return (string)this["timeoutUrl"]; }
            set { this["timeoutUrl"] = value; }
        }

        /// <summary>
        /// Gets or sets the URL of the page used when Live ID ticket is not linked to membership user.
        /// </summary>
        /// <value>The URL of the page used when Live ID ticket is not linked to membership user.</value>
        [ConfigurationProperty("unlinkedUrl", IsRequired = false, DefaultValue = "~/Login.aspx?Reason=unlinked")]
        public string UnlinkedUrl {
            get { return (string)this["unlinkedUrl"]; }
            set { this["unlinkedUrl"] = value; }
        }


    }

    /// <summary>
    /// Class representing configuration of the Windows Live ID Service communication.
    /// </summary>
    public class ServiceElement : ConfigurationElement {

        /// <summary>
        /// Gets or sets the application ID.
        /// </summary>
        /// <value>The application ID.</value>
        [ConfigurationProperty("applicationId", IsRequired = true, DefaultValue = "0000000000000000")]
        [RegexStringValidator("^[0-9A-F]{16}$")]
        public string ApplicationId {
            get { return (string)this["applicationId"]; }
            set { this["applicationId"] = value; }
        }

        /// <summary>
        /// Gets or sets the secret key configured in Windows Live registration.
        /// </summary>
        /// <value>The secret key configured in Windows Live registration.</value>
        [ConfigurationProperty("secretKey", IsRequired = true, DefaultValue = "0000000000000000")]
        [StringValidator(InvalidCharacters = "<>%;", MinLength = 16, MaxLength = 64)]
        public string SecretKey {
            get { return (string)this["secretKey"]; }
            set { this["secretKey"] = value; }
        }

        /// <summary>
        /// Gets or sets the sign-in algorithm.
        /// </summary>
        /// <value>The sign-in algorithm.</value>
        [ConfigurationProperty("algorithm", IsRequired = false, DefaultValue = "wsignin1.0")]
        public string Algorithm {
            get { return (string)this["algorithm"]; }
            set { this["algorithm"] = value; }
        }

    }

}
